<?php


namespace App\Repositories\Ota\Ctrip\Policy\Eloquent;


use App\Repositories\Ota\Ctrip\Policy\Contracts\SpecialPolicyInterface;
use App\Repositories\Policy\Eloquent\PolicyRepository;

class SpecialPolicyRepository extends PolicyRepository implements SpecialPolicyInterface
{


    /**
     * 特殊政策ID列表查询请求
     * @param $param
     * @return mixed|void
     */
    public function idLists($param = [])
    {
        $this->MessageBody['PageSize'] = 1000000;
        return $this->req('SpecialPolicyIDList',array('MessageHead'=>$this->MessageHead,'MessageBody'=>$this->MessageBody),1);
    }

    /**
     * 特殊政策概要信息查询请求
     * @param array $param
     * @return mixed
     */
    public function summary($param = [])
    {
        $this->searchType($param);
        $this->MessageBody['PageSize'] = 1000;


        return $this->req('SpecialPolicySummary',array('MessageHead'=>$this->MessageHead,'MessageBody'=>$this->MessageBody),1);
    }

    /**
     * 特殊政策概要信息查询请求
     * @param array $param
     * @return mixed
     */
    public function summaryMh($param = [])
    {
        $this->MessageBody['PageSize'] = 1000;
        return $this->req('SpecialPolicySummary',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody),1);
    }

    /**
     * 特殊政策详细信息查询请求
     * @param array $param
     * @return mixed
     */
    public function detailSearch($param = []){

        $this->MessageBody['PolicyIDList'] = $param;
        return $this->req('SpecialPolicyDetailSearch',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody),1);
    }

    /**
     * 特殊政策详细信息查询请求
     * @param array $param
     * @return mixed
     */
    public function detailMhSearch($param = []){

        $this->MessageBody['PolicyIDList'] = $param;
        return $this->req('SpecialPolicyDetailSearch',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody),1);
    }

    /**
     * 特殊政策删除请求
     * @param $param
     * @param string $type
     * @param array $isFuzzy
     * @return mixed|void
     */
    public function delete($param,$type='ByID',$isFuzzy='T'){

        $this->MessageBody =  [
            "DeleteType"=>$type,
            /*"PolicyIDList"=>$param,*/
            /*"PolicyCodeList"=>$param,*/
            /*"ExternalIDList"=>[
                0
            ],*/
            "PolicySource"=>"ALL",
            "IsFuzzyMatch"=>$isFuzzy
        ];

        if ($type == 'ByID') {
            $this->MessageBody['PolicyIDList'] = $param;
        } else {

            $this->MessageBody['PolicyCodeList'] = $param;
        }

        return $this->req('SpecialPolicyDelete',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 特殊政策删除请求
     * @param $param
     * @param string $type
     * @param array $isFuzzy
     * @return mixed|void
     */
    public function deleteMh($param,$type='ByID',$isFuzzy='T'){

        $this->MessageBody =  [
            "DeleteType"=>$type,
            /*"PolicyIDList"=>$param,*/
            /*"PolicyCodeList"=>$param,*/
            /*"ExternalIDList"=>[
                0
            ],*/
            "PolicySource"=>"ALL",
            "IsFuzzyMatch"=>$isFuzzy
        ];

        if ($type == 'ByID') {
            $this->MessageBody['PolicyIDList'] = $param;
        } else {

            $this->MessageBody['PolicyCodeList'] = $param;
        }

        return $this->req('SpecialPolicyDelete',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 特殊政策删除请求
     * @param $param
     * @param string $type
     * @param array $lists
     * @return mixed|void
     */
    public function deleteZh($param,$type='ByID',$isFuzzy='T'){

        $this->MessageBody =  [
            "DeleteType"=>$type,
            /*"PolicyIDList"=>$param,*/
            "PolicyCodeList"=>$param,
            /*"ExternalIDList"=>[
                0
            ],*/
            "PolicySource"=>"ALL",
            "IsFuzzyMatch"=>$isFuzzy
        ];
        return $this->req('SpecialPolicyDelete',array('MessageHead'=>$this->MessageHead,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 特殊政策新增请求
     * @param $param
     * @return mixed|void
     */
    public function add($param)
    {

        $this->formatData($param);
        return $this->req('SpecialPolicyAdd',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 最慧特殊政策新增请求
     * @param $param
     * @return mixed|void
     */
    public function addZh($param)
    {

        $this->formatData($param);
        return $this->req('SpecialPolicyAdd',array('MessageHead'=>$this->MessageHead,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 美惠特殊政策新增请求
     * @param $param
     * @return mixed
     */
    public function addMh($param){

        $this->formatData($param);
        return $this->req('SpecialPolicyAdd',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 特殊政策修改请求
     * @param $param
     * @return mixed
     */
    public function change($param)
    {

        $this->formatData($param);
        return $this->req('SpecialPolicyChange',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 特殊政策修改请求
     * @param $param
     * @return mixed
     */
    public function changeMh($param){
        $this->formatData($param);
        return $this->req('SpecialPolicyChange',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 修改政策价格
     * @param $param
     * @return mixed
     */
    public function changePrice($param){

        if (count($param,1) == count($param)) {

            $param = [$param];
        }

        $PolicyList = [];
        foreach ($param as $key=>$value) {

            $policy = [
                "SummaryUnit" => [
                    "IsValid"=>env('POLICY_VALID') ? $value['IsValid'] : 'F',
                ],
            ];

            $policy[ 'PolicyID'] = $value['PolicyID'];

            $PolicyList[] = $policy;
        }

        $this->MessageBody = [
            "PolicyList"=>$PolicyList
        ];

        return $this->req('SpecialPolicyChangePrice',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 修改政策价格
     * @param $param
     * @return mixed
     */
    public function changeMhPrice($param){

        if (count($param,1) == count($param)) {

            $param = [$param];
        }

        $PolicyList = [];
        foreach ($param as $key=>$value) {

            $policy = [
                "SummaryUnit" => [
                    "IsValid"=>env('POLICY_VALID') ? $value['IsValid'] : 'F',
                ],
            ];

            $policy[ 'PolicyID'] = $value['PolicyID'];

            $PolicyList[] = $policy;
        }

        $this->MessageBody = [
            "PolicyList"=>$PolicyList
        ];

        return $this->req('SpecialPolicyChangePrice',array('MessageHead'=>$this->MessageHead,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 特殊政策数据格式化
     * @param $param
     */
    private function formatData($param){

        if (count($param,1) == count($param)) {

            $param = [$param];
        }

        $PolicyList = [];
        foreach ($param as $key=>$value) {

            $policy = [
                "SummaryUnit"=>[
                    "SupplierPolicyID"=>0,
                    "Class"=>$value['Class'],
                    "AdvanceDay"=>$value['AdvanceDay'],
                    "MaxAdvanceDay"=>isset($value['MaxAdvanceDay']) ? $value['MaxAdvanceDay'] : $value['AdvanceDay'],
                    "RcID"=>!empty($value['RcID'])? $value['RcID']: '',
                    "PolicyCode"=>$value['PolicyCode'],
                    "AirLine"=>$value['AirLine'],
                    "FlightWay"=>$value['FlightWay'],
                    "ApplyChild"=>$value['ApplyChild'],
                    "DepartPort"=>$value['DepartPort'],
                    "ArrivePort"=>$value['ArrivePort'],
                    "EffectDate"=>'/Date('.strtotime($value['EffectDate']).'000+0800)/',
                    "ExpiryDate"=>'/Date('.strtotime($value['ExpiryDate']).'000+0800)/',
                    "MinStopDay"=>!empty($value['MinStopDay']) ? $value['MinStopDay'] : NULL,
                    "MaxStopDay"=> !empty($value['MaxStopDay']) ? $value['MaxStopDay'] : NULL,
                    "IsValid"=>env('POLICY_VALID') ? $value['IsValid'] : 'F',
                    "ProductType"=>$value['ProductType'],
                    "SaleTimeLimit"=>!empty($value['SaleTimeLimit']) ? $value['SaleTimeLimit'] : '',
                    "DateCanSale"=>!empty($value['DateCanSale']) ? $value['DateCanSale'] : "T",
                    "VipCode"=>!empty($value['VipCode']) ? $value['VipCode'] : '',
                    "NeedPATA"=>!empty($value['NeedPATA']) ? $value['NeedPATA'] : "F",
                    "PataBasePrice"=>!empty($value['PataBasePrice']) ? $value['PataBasePrice'] : NULL,
                    "PataPriceValue"=>!empty($value['PataPriceValue']) ? $value['PataPriceValue'] : NULL,
                    "DownFloatValue"=>!empty($value['DownFloatValue']) ? $value['DownFloatValue'] : NULL,
                    'PataVerifyPriceOption'=>isset($value['PataVerifyPriceOption']) ? $value['PataVerifyPriceOption'] : NULL,
                    "IATANo"=>!empty($value['IATANo']) ? $value['IATANo'] : NULL,
                    'TicketRemark'=>!empty($value['TicketRemark']) ? $value['TicketRemark'] : NULL,
                    'IsShareFlight'=>!empty($value['IsShareFlight']) ? $value['IsShareFlight'] : 'F',
                    'IsValidCarrierAirline'=>!empty($value['IsValidCarrierAirline']) ? $value['IsValidCarrierAirline'] : 'F',
                    'Sourcefrom'=>!empty($value['Sourcefrom']) ? $value['Sourcefrom']: NULL,
                ],

                "InventoryUnit"=>[
                    "InventoryType"=>$value['InventoryType'],
                    'IsShortageFilter'=> !empty($value['IsShortageFilter']) ? $value['IsShortageFilter'] : 'F',
                    'ShortageFilterCount' => !empty($value['ShortageFilterCount']) ? $value['ShortageFilterCount'] : 0,
                ],
                "GoFlightUnit"=>[
                    "EffectDate"=>'/Date('.strtotime($value['GoEffectDate']).'000+0800)/',
                    "ExpiryDate"=>'/Date('.strtotime($value['GoExpiryDate']).'000+0800)/',
                    "Days"=> !empty($value['Days']) ? implode('',$value['Days']) : '1234567',
                    "SubClass"=>isset($value['SubClass']) ? $value['SubClass'] : NULL,
                    "IsCanSale"=>!empty($value['IsCanSale']) ? $value['IsCanSale'] : 'T',
                    "RuleDetail"=>!empty($value['RuleDetail']) ? $value['RuleDetail'] : '',
                    "DepartTimeLimit"=>!empty($value['DepartTimeLimit']) ? $value['DepartTimeLimit'] : NULL,
                    "ExcludeRouteDate"=>!empty($value['ExcludeRouteDate']) ? $value['ExcludeRouteDate'] : NULL,
                ],
                "ConditionUnit"=>[
                    "PayTypeList"=>!empty($value['PayTypeList']) ? $value['PayTypeList'] : [],
                    "CardTypeList"=>!empty($value['CardTypeList']) ? $value['CardTypeList'] : [],
                ],
                "PriceUnit"=>[
                    "IsCompositionPrice"=> $value['IsCompositionPrice'] == 'T' ? true : false,
                    "PrintPrice"=>$value['PrintPrice'],
                    "SalePrice"=>isset($value['SalePrice']) ? $value['SalePrice'] : NULL,
                    "SetPrice"=>!empty($value['SetPrice']) ? $value['SetPrice'] : 0,
                    "ReturnPrice"=>!empty($value['ReturnPrice']) ? $value['ReturnPrice'] : 0,
                    "ReturnPoint"=>!empty($value['ReturnPoint']) ? $value['ReturnPoint'] : 0,
                    "BiddingSpace"=>!empty($value['BiddingSpace']) ? $value['BiddingSpace'] : 0,
                    "BidType"=>!empty($value['BidType']) ? $value['BidType'] : 0
                ],

            ];

            if (!empty($value['OfficeNo'])) {

                $policy['SummaryUnit']['OfficeNo'] = $value['OfficeNo'];
            }

            //设置定向
            if (isset($value['HideSales']) && $value['HideSales'] == 'T') {

                $policy['SummaryUnit']['passengersNum'] = !empty($value['passengersNum']) ? $value['passengersNum'] : NULL;
                $policy['SummaryUnit']['RefundChange'] = !empty($value['RefundChange']) ? $value['RefundChange'] : NULL;
            }

            //销售价格范围
            if (!empty($value['SalePriceMin']) && !empty($value['SalePriceMax'])) {

                $policy['SummaryUnit']['SalePriceMin'] = $value['SalePriceMin'];
                $policy['SummaryUnit']['SalePriceMax'] = $value['SalePriceMax'];
            }

            //处理紧张定位阈值
            if ($value['IsBooking'] == '') {
                $policy['InventoryUnit']["IsBooking"] =  'T';
                $policy['InventoryUnit']['BookingThreshold'] = $value['BookingThreshold'];
            } else {

                $policy['InventoryUnit']["IsBooking"] = !empty($value['IsBooking']) ? $value['IsBooking'] : 'F';
            }

            //往返程设置
            if ($value['FlightWay'] == 'D'){

                $policy['BackFlightUnit'] = [
                    "EffectDate"=>'/Date('.strtotime($value['BackEffectDate']).'000+0800)/',
                    "ExpiryDate"=>'/Date('.strtotime($value['BackExpiryDate']).'000+0800)/',
                    "Days"=> !empty($value['BackDays']) ? implode('',$value['BackDays']) : '1234567',
                    "SubClass"=>isset($value['BackSubClass']) ? $value['BackSubClass'] : NULL,
                    "IsCanSale"=>!empty($value['BackIsCanSale']) ? $value['BackIsCanSale'] : 'T',
                    "RuleDetail"=>!empty($value['BackRuleDetail']) ? $value['BackRuleDetail'] : '',
                    "DepartTimeLimit"=>!empty($value['BackDepartTimeLimit']) ? $value['BackDepartTimeLimit'] : '',
                    "ExcludeRouteDate"=>!empty($value['BackExcludeRouteDate']) ? $value['BackExcludeRouteDate'] : NULL,
                ];
            }

            //证件设置
            if ($value['IDLimit'] == 'T') {
                $policy['ConditionUnit']["IDCardList"] = !empty($value['IDCardList']) ? $value['IDCardList'] : [];
            }

            //退改设置
            if (empty($policy['SummaryUnit']['RcID'])) {

                $policy['RuleRestrict']= [
                    "FeeBasis"=> !empty($value['FeeBasis']) ? $value['FeeBasis'] : '',
                    "MinFeeExpense" => !empty($value['MinFeeExpense']) ? $value['MinFeeExpense'] : 0,
                    "RefundRateRemarks" => !empty($value['RefundRateRemarks']) ? $value['RefundRateRemarks'] : '0',
                    "ReroutRateRemarks" => !empty($value['ReroutRateRemarks']) ? $value['ReroutRateRemarks'] : '0',
                    "IsEndorse" => !empty($value['IsEndorse']) ? $value['IsEndorse'] : 'F',
                ];

            }

            //设置库存
            if ($value['TicketInventory']) {

                $policy['InventoryUnit']['FIX'] =[
                    "SaledTicketNum"=>!empty($value['SaledTicketNum']) ? $value['SaledTicketNum'] : 0,
                    "TicketInventory"=> $value['TicketInventory'] ?  : NULL,
                ];

                if ($value['InventoryType'] == 'FIX') {

                    $policy['InventoryUnit']['IsReturnRefundInventory'] = !empty($value['IsReturnRefundInventory']) ? $value['IsReturnRefundInventory'] : 'F';
                }
            }

            if (!empty($value['PolicyID'])) {

                $policy['PolicyID'] = $value['PolicyID'];
            }

            $PolicyList[] = $policy;
        }

        $this->MessageBody = [
            "PolicyList"=>$PolicyList
        ];
    }

    /**
     * 查询低价
     * @param $param
     * @return mixed
     */
    public function lowPricePolicyList($param){


        $this->MessageBody =  [
            'SearchCondition' => [
                'AirLine'       => !empty($param['AirLine']) ? $param['AirLine'] : NULL,
                'GoSubClass'    => !empty($param['GoSubClass']) ? $param['GoSubClass'] : NULL,
                'EffectDate'    => $param['EffectDate'],
                'ExipryDate'    => $param['ExipryDate'],
                'DepartPort'    => $param['DepartPort'],
                'ArrivePort'    => $param['ArrivePort'],
                'SaleChannel'   => $param['SaleChannel'],
                'TradePolicyType'   => $param['TradePolicyType'],
                'ShareProductTypeList' => $param['ShareProductTypeList']
            ],
            'Options'=>['Engine']
        ];

        return $this->lowPriceReq('LowPricePolicyList',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 查询低价
     * @param $param
     * @return mixed
     */
    public function lowPricePolicyMhList($param){


        $this->MessageBody =  [
            'SearchCondition' => [
                'AirLine'       => !empty($param['AirLine']) ? $param['AirLine'] : NULL,
                'GoSubClass'    => !empty($param['GoSubClass']) ? $param['GoSubClass'] : NULL,
                'EffectDate'    => $param['EffectDate'],
                'ExipryDate'    => $param['ExipryDate'],
                'DepartPort'    => $param['DepartPort'],
                'ArrivePort'    => $param['ArrivePort'],
                'SaleChannel'   => $param['SaleChannel'],
                'TradePolicyType'   => $param['TradePolicyType'],
                'ShareProductTypeList' => $param['ShareProductTypeList']
            ],
            'Options'=>['Engine']
        ];

        return $this->lowPriceReq('LowPricePolicyList',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody));
    }
}